//
//  HMNavigationController.swift
//  Weibo
//
//  Created by ZhuDong on 16/5/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMNavigationController: UINavigationController,UIGestureRecognizerDelegate {
    
    override func viewDidLoad() {
        //
        self.interactivePopGestureRecognizer?.delegate = self
    }
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        //顶级视图不能使用
        return childViewControllers.count > 1
    }
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        if childViewControllers.count > 0 {
            var backTitle = "返回"
            
            if childViewControllers.count == 1 {
               backTitle = childViewControllers[0].navigationItem.title ?? "返回"
            }
            let back = UIBarButtonItem(title: backTitle, imageName: "navigationbar_back_withtext", target: self, action: #selector(backBtnClick))
            viewController.navigationItem.leftBarButtonItem = back
        }
        super.pushViewController(viewController, animated: animated)
       
    }
    
    @objc private func backBtnClick() {
        popViewControllerAnimated(true)
    }
}
